package tr.com.netgis.pojo;

import java.io.Serializable;
import java.text.MessageFormat;

import tr.com.netgis.util.Utility;
import android.os.Parcel;
import android.os.Parcelable;

/**
 * 
 * @author NetGis
 * 
 */
public class Message implements Parcelable, Serializable {

	public static enum Type {
		COMMAND,
		MESSAGE;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2480343296389696754L;

	public static class Command {
		private tr.com.netgis.util.Command command = tr.com.netgis.util.Command.UNKNOWN;
		private String value = "";

		public tr.com.netgis.util.Command getCommand() {
			return command;
		}

		public void setCommand(tr.com.netgis.util.Command command) {
			this.command = command;
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		private Command(tr.com.netgis.util.Command command, String value) {
			super();
			this.command = command;
			this.value = value;
		}

		private Command() {
			super();
		}

	}

	public static final Creator<Message> CREATOR = new Creator<Message>() {

		@Override
		public Message createFromParcel(Parcel source) {
			return new Message(source);
		}

		@Override
		public Message[] newArray(int size) {
			return new Message[size];
		}

	};	
	
	private Message(Parcel source) {
		this._id = source.readInt();
		this.command = source.readString();
		this.commandParameter = source.readString();
		/*
		this.messageBody = source.readString();
		this.messageHeader = source.readString();
		*/
		this.sender = source.readString();
		this.time = source.readString();
		this.read = source.readInt();
	}
	
	private int _id;
	private String sender;
	private String time;
	private String command;
	private String commandParameter;
	private int read = 0;

	public Message() {
		
	}
	
	public boolean isRead() {
		return read == 1;
	}
	
	public int getRead() {
		return read;
	}

	public void setRead(int read) {
		this.read = read;
	}

	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public Type getType() {
		if ("0".equals(this.command))
			return Type.MESSAGE;
		return Type.COMMAND;
	}


	/**
	 * Header:message_header;Body:message_body
	 * @return
	 */
	public String getMessageHeader() {
		if (this.getType().equals(Type.MESSAGE)) {
			String [] strArr =  this.commandParameter.split("\\|");
			String headerStr = strArr[0];
			return headerStr;
			//return headerStr.substring(headerStr.indexOf(":") + 1);
		}
		return "";
	}

	/**
	 * Header:message_header;Body:message_body
	 * @return
	 */
	public String getMessageBody() {
		if (this.getType().equals(Type.MESSAGE)) {
			String [] strArr =  this.commandParameter.split("\\|");
			String bodyStr = strArr[1];
			return bodyStr;
			//return bodyStr.substring(bodyStr.indexOf(":") + 1);
		}
		return "";
	}


	public String getCommand() {
		return command;
	}

	public void setCommand(String command) {
		this.command = command;
	}

	public String getCommandParameter() {
		return commandParameter;
	}

	public void setCommandParameter(String commandParameter) {
		this.commandParameter = commandParameter;
	}
	
	public boolean isCommand (){
		return Type.COMMAND.equals(this.getType());
	}

	public boolean isMessage (){
		return Type.MESSAGE.equals(this.getType());
	}
	
	public Command getOutCommand (){
		if (!this.getType().equals(Type.COMMAND))
			return new Command(tr.com.netgis.util.Command.UNKNOWN, null);
		Command command = new Command();
		int code = -99;
		try {
			code = Integer.parseInt(this.command);
		} catch (Exception e) {
		}
		command.setCommand(tr.com.netgis.util.Command.valueOf(code));
		command.setValue(this.commandParameter);
		return command;
	}

	public String toString() {
		return MessageFormat
				.format("[sender:{0}, time: {1}, type: {2}, message-header: {3},"
						+ "message-body: {4}, command: {5}, command-parameter: {6}]",
						sender, time, getType(), getMessageHeader(), getMessageBody(),
						command, commandParameter);
	}
	
	/**
	 * for view in list
	 * @return
	 */
	public String viewMessageHeader() {
		if (this.isMessage())
			return this.getMessageHeader();
		else if (this.isCommand()) {
			return Utility.getFriendlyCommand(getOutCommand().getCommand());
		}
		return "Bilinmeyen";
	}
	
	/**
	 * for view in list
	 * @return
	 */
	public String viewMessageBody() {
		if (this.isMessage())
			return this.getMessageBody();
		else if (this.isCommand()) {
			return this.commandParameter;
		}
		return "Bilinmeyen";
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(this._id);
		dest.writeString(this.command);
		dest.writeString(this.commandParameter);
		dest.writeString(this.sender);
		dest.writeString(this.time);
		dest.writeInt(this.read);
	}

	@Override
	public boolean equals(Object o) {
		if (o instanceof Message) {
			return this._id == ((Message)o)._id;
		}
		return false;
	}
	
	
}